<template>
  <div>
    <!-- 定义卡片视图 -->
    <el-card class="box-card">

      <!-- 定义页面提示信息 -->
      <el-alert title="新增商品流程" type="info" effect="dark" center show-icon :closable="false"></el-alert>

      <!-- 定义步骤条 字符串需要转化为数值 -->
      <el-steps :active="activeIndex - 0" finish-status="success" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>

      <!-- 定义标签页 before-leave:切换标签之前的钩子，若返回 false 或者返回 Promise 且被 reject，则阻止切换。 -->
      <el-form :model="addItemForm" :rules="addItemFormRules" ref="addItemFormRef" label-width="100px"
        label-position="top">
        <el-tabs v-model="activeIndex" :tab-position="'left'">
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="商品标题" prop="name">
              <el-input v-model="addItemForm.name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格" prop="price">
              <el-input v-model="addItemForm.price"></el-input>
            </el-form-item>
            <el-form-item label="商品类型" prop="hotId">
              <el-input v-model="addItemForm.hotId"></el-input>
            </el-form-item>
          </el-tab-pane>

          <!-- 实现图片上传 multiple支持多选文件 -->
          <el-tab-pane label="商品图片" name="1">
            <el-upload class="upload-demo" :action="uploadUrl" :on-preview="handlePreview"
              :on-success="handleSuccess" list-type="picture" multiple drag>
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过1M</div>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品详情" name="2">
            <!-- 定义富文本编辑器-->
            <quill-editor ref="myQuillEditor" v-model="itemDesc.itemDesc">
            </quill-editor>

            <!-- 定义添加商品按钮-->
            <el-button type="primary" class="addItemBtnClass" @click="addItemBtn">添加商品</el-button>
          </el-tab-pane>
        </el-tabs>
      </el-form>

    </el-card>

    <!-- 定义图片展现对话框 -->
    <el-dialog title="图片预览" :visible.sync="dialogImageVisible">
      <img :src="imageUrlPath" width="100%" height="100%" />
    </el-dialog>

  </div>
</template>


<script>
export default {
  data () {
    return {
      activeIndex: '0',
      addItemForm: {
        name: '',
        price: 0,
        hotId: 1,
        photoFile: []
      },
      itemDesc: {
        itemDesc: ''
      },
      itemParam: {
        dynamicArray: [],
        staticArray: [],
        dynamicArgs: '',
        staticArgs: ''
      },
      addItemFormRules: {
        name: [{
          required: true,
          message: '请输入商品标题信息',
          trigger: 'blur'
        }],
        price: [{
          required: true,
          message: '请输入商品价格信息',
          trigger: 'blur'
        }],
        HotId: [{
          required: true,
          message: '请输入商品类型信息',
          trigger: 'blur'
        }]

      },
      props: {
        expandTrigger: 'hover',
        value: 'id',
        label: 'name',
        children: 'children'
      },
      dynamicTableData: [],
      staticTableData: [],

      uploadUrl: 'http://localhost:8092/aj-file/addUpload',
      imageUrlPath: '',
      dialogImageVisible: false
    }
  },
  methods: {
    handlePreview (file) {
      // 获取图片的虚拟路径
      this.imageUrlPath = file.response.data.urlPath
      this.dialogImageVisible = true
    },
    handleSuccess (response, file) {
      if (response.status !== 200) return this.$message.error('文件上传失败')
      this.addItemForm.photoFile=response.data
    },
    async addItemBtn () {
      this.$refs.addItemFormRef.validate(valid => {
        if (!valid) return this.$message.error('请输入商品必填项')
      })
      const itemVO = {
        itemList: this.addItemForm,
        details: this.itemDesc.itemDesc
      }
      const { data: result } = await this.$http.post('/aj-item/saveItem', itemVO)
      if (result.status !== 200) return this.$message.error('商品添加失败')
      this.$message.success('商品添加成功')
      // 2.5添加完成之后,将数据重定向到商品展现页面
      this.$router.push('/admin')
    }
  }
}
</script>

<style>
  .el-steps {
    margin: 20px 0;
  }

  .el-cascader {
    width: 30%;
  }

  /* 控制复选框的右边距5像素 !important 表示优先执行 IE6不兼容*/
  .el-checkbox {
    margin: 0 10px 0 0 !important;
  }

  .addItemBtnClass{

    margin-top: 15px;
  }

</style>
